package com.lanou.utp.controller;

import com.lanou.utp.client.SsoClient;
import com.lanou.utp.entity.Admin;
import com.lanou.utp.entity.LoginUser;
import com.lanou.utp.entity.Student;
import com.lanou.utp.entity.Teacher;
import com.lanou.utp.service.UserService;
import com.lanou.utp.entity.ResponseData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class UserController {

    @Resource
    private SsoClient ssoClient;

    @Resource
    private UserService userService;

    @RequestMapping("/user")
    @ResponseBody
    public ResponseData getCurrentUser(@RequestHeader("token") String token) {
        LoginUser user = ssoClient.getCurrentUser(token);
        Integer id = user.getId();
        String username = user.getName();
        Integer channel = user.getChannel();
        Integer position = user.getPosition();

        // 管理员/教师/学生
        if (position == 0) {
            Admin admin = userService.getAdmin(id);
            return new ResponseData("ok", admin);
        } else if (position == 1) {
            Teacher teacher = userService.getTeacher(id);
            return new ResponseData("ok", teacher);
        } else if (position == 2) {
            Student student = userService.getStudent(id);
            return new ResponseData("ok", student);
        } else {
            // TODO: 2019/12/20 异常抛出
            return null;
        }

    }

}
